package com.houmeiyuan.huiguniang.service;

import com.houmeiyuan.huiguniang.modles.dao.ProductDao;
import com.houmeiyuan.huiguniang.modles.dao.ShopCarDao;
import com.houmeiyuan.huiguniang.modles.domain.Product;
import com.houmeiyuan.huiguniang.modles.domain.ProductDetail;
import com.houmeiyuan.huiguniang.modles.domain.ShopCar;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Service
public class ProductService {
    @Autowired
    private ProductDao productDao;
    @Autowired
    private ShopCarDao shopCarDao;

    public Integer updateSellNumber(Integer productNumber){
        Map<String, Object> params = new HashMap<>();
        List<ShopCar> shopCarList = shopCarDao.searchByProductNumber(productNumber);
        boolean flag = true;
        for (ShopCar shopCar:shopCarList) {
            params.put("num",shopCar.getProductNumber());
            params.put("productId",shopCar.getProductId());
            if(productDao.updateSellNumber(params)==0){
                flag = false;
            }
            params.clear();
        }
        if(flag){
            return 1;
        }
        return 0;
    }
    /**
     * @author lilanlan
     * @date 2019-08-11 20:09
     */
    @Transactional
    public  ProductDetail fetchByProductId(Map<String, Object> params){

        return productDao.fetchByProductId(params);

    }

    /**
     * @fanfan 2019/8/21 根据id返回商品信息
     * @param id
     * @return
     */
    public  Product selectById(Integer id)
   {
       return productDao.selectById(id);
   }
}
